#include "StdAfx.h"
#include "ListTextElementUI.h"
#include "BlueRenderEngineUI.h"

CListTextElementUI::CListTextElementUI(void): m_cyItem(0), m_nLinks(0), m_pOwner(NULL)
{
	::ZeroMemory(&m_rcLinks, sizeof(m_rcLinks));
}

CListTextElementUI::~CListTextElementUI(void)
{
}

LPCTSTR CListTextElementUI::GetClass() const
{
	return _T("ListTextElementUI");
}

UINT CListTextElementUI::GetControlFlags() const
{
	return UIFLAG_WANTRETURN | (m_nLinks > 0 ? UIFLAG_SETCURSOR : 0);
}

void CListTextElementUI::SetOwner(CControlUI* pOwner)
{
	CListElementUI::SetOwner(pOwner);
	m_pOwner = static_cast<IListUI*>(pOwner->GetInterface(L"List"));
}

void CListTextElementUI::Event(TEventUI& event)
{
	// When you hover over a link
	if( event.Type == UIEVENT_SETCURSOR ) {
		for( int i = 0; i < m_nLinks; i++ ) {
			if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
				::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
				return;
			}
		}      
	}
	if( m_nLinks > 0 && event.Type == UIEVENT_MOUSEMOVE ) Invalidate();
	if( m_nLinks > 0 && event.Type == UIEVENT_MOUSELEAVE ) Invalidate();
	CListLabelElementUI::Event(event);
}

SIZE CListTextElementUI::EstimateSize(SIZE szAvailable)
{
	if( m_pOwner == NULL ) return CStdSize();
	// We calculate the item height only once, because it will not wrap on the
	// line anyway when the screen is resized.
	if( m_cyItem == 0 ) {
		LPCTSTR pstrText = _T("XXX");
		RECT rcText = { 0, 0, 9999, 9999 };
		int nLinks = 0;
		CBlueRenderEngineUI::DoPaintPrettyText(m_pManager->GetPaintDC(), m_pManager, rcText, pstrText, UICOLOR__INVALID, UICOLOR__INVALID, NULL, nLinks, DT_CALCRECT | m_uTextStyle);
		m_cyItem = rcText.bottom - rcText.top;
	}
	return CStdSize(m_cxWidth, m_cyItem + 9);
}

void CListTextElementUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	DrawItem(hDC, m_rcItem, 0);
	
}

void CListTextElementUI::DrawItem(HDC hDC, const RECT& rcItem, UINT uStyle)
{
	ASSERT(m_pOwner);
	if( m_pOwner == NULL ) return;
	const TListInfoUI* pInfo = m_pOwner->GetListInfo();
	UITYPE_COLOR iTextColor = pInfo->Text;
	UITYPE_COLOR iBackColor = pInfo->Background;
	if( (m_uButtonState & UISTATE_HOT) != 0 ) {
		iBackColor = pInfo->HotBackground;
		iTextColor = pInfo->HotText;
	}
	if( IsSelected() ) {
		iTextColor = pInfo->SelText;
		iBackColor = pInfo->SelBackground;
	}
	if( !IsEnabled() ) {
		iTextColor = UICOLOR_CONTROL_TEXT_DISABLED;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_DISABLED;
	}
	if( iBackColor != UICOLOR__INVALID ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, m_rcItem, iBackColor);
	}
	IListCallbackUI* pCallback = m_pOwner->GetTextCallback();
	ASSERT(pCallback);
	if( pCallback == NULL ) return;
	m_nLinks = 0;
	int nLinks = lengthof(m_rcLinks);
	for( int i = 0; i < pInfo->nColumns; i++ )
	{
		// Paint text
		RECT rcItem;
		if(i == 0)
		{
			rcItem.left  = pInfo->rcColumn[i].left + 30;
			rcItem.top = m_rcItem.top;
			rcItem.right = pInfo->rcColumn[i].right + 30;
			rcItem.bottom = m_rcItem.bottom - 1;
		}
		else
		{
			rcItem.left  = pInfo->rcColumn[i].left;
			rcItem.top = m_rcItem.top;
			rcItem.right = pInfo->rcColumn[i].right;
			rcItem.bottom = m_rcItem.bottom - 1;
		}
		//RECT rcItem = { pInfo->rcColumn[i].left, m_rcItem.top, pInfo->rcColumn[i].right, m_rcItem.bottom - 1 };
		if(m_imgIco == NULL)
		{
			LPCTSTR pstrIco = pCallback->GetImgItem(this,m_iIndex);
			if(StrCmpI(pstrIco,L"") != 0)
			{
				m_imgIco.Load(pstrIco);
			}
		}
		
		LPCTSTR pstrText = pCallback->GetItemText(this, m_iIndex, i);
		::InflateRect(&rcItem, -4, 0);
		//CBlueRenderEngineUI::DoPaintAlphaBitmap(hDC,m_pManager,,rcItem,255);
		CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcItem, pstrText, iTextColor, UICOLOR__INVALID, m_rcLinks, nLinks, DT_SINGLELINE | m_uTextStyle);
		if( nLinks > 0 ) m_nLinks = nLinks, nLinks = 0; else nLinks = lengthof(m_rcLinks);
	}
	if(m_imgIco)
		m_imgIco.TransparentBlt(hDC,m_rcItem.left + 10,m_rcItem.top + 3,m_imgIco.GetWidth(),m_imgIco.GetHeight(),RGB(0,0,0));
	RECT rcLine = { m_rcItem.left, m_rcItem.bottom - 1, m_rcItem.right, m_rcItem.bottom - 1 };
	CBlueRenderEngineUI::DoPaintLine(hDC, m_pManager, rcLine, UICOLOR_DIALOG_BACKGROUND);
	
}

